package light.head.controller.oa;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AnnCollectDAO;
import light.head.dao.oa.AnnouncementDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.AnnCollect;
import light.head.model.Announcement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_ANNCOLLECT)
public class AnnouncementCollectController extends SupporterController{
	
	private AnnouncementDAO annDAO;
	private AnnCollectDAO annCDAO;
	private OrganDAO organDAO;
	

	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ANNCOLLECT_INIT);
	}

	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Integer oc  = organDAO.getRoot(cu_organCode(request)).getCode();
		if(type.equals(Sys.S_ONE)) {
			Map<String, String> map = createQueryMapByParmName(request, Parm.RT, Parm.OC, Parm.START, Parm.END, Parm.TITLE, Parm.EDITION,Parm.CUNAME);
			Boolean a = true;
			map.put(Parm.EDITION, a.toString());
			
			if (annDAO.findByMap(organDAO.getSonCodeList(oc),map, pb) == RESULT.SUCCESS) {
				Announcement ann = null;
				for (Object o : pb.getResultList()) {
					ann = (Announcement) o;
					ann.setCollcetList(annCDAO.findByCodeUser(ann.getCode(), cu_code(request)));
				}
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			return new ModelAndView(Url.ANNCOLLECT_LIST);
		}else {
			Map<String, String> map = createQueryMapByParmName(request, Parm.UC, Parm.OC, Parm.START, Parm.END, Parm.TITLE, Parm.EDITION, Parm.DELETED,Parm.CUNAME);
			map.put(Parm.USER, cu_code(request).toString());
			map.put(Parm.UC, cu_code(request).toString());
			if (annDAO.findByMap1(organDAO.getSonCodeList(oc),cu_code(request),map, pb) == RESULT.SUCCESS) {
				Announcement ann = null;
				for (Object o : pb.getResultList()) {
					ann = (Announcement) o;
					ann.setCollcetList(annCDAO.findByCodeUser(ann.getCode(), cu_code(request)));
				}
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			return new ModelAndView(Url.ANNCOLLECT_LISTCOLLECT);
		}
		
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)) {
			Announcement ann = annDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.ANN, ann);
		}
		return new ModelAndView(Url.ANNCOLLECT_SHOW);
	}
	
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		AnnCollect annc =(AnnCollect) annCDAO.findByCodeUser(Integer.parseInt(code), cu_code(request)).get(0);
		if (null == annc) {
			print(response, Msg.NOT_FIND);
			return;
		}

		annc.setDeleted(true);
		if (annCDAO.upd(annc) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		AnnCollect annc = annCDAO.findByCodeUser(Integer.parseInt(code), cu_code(request)).get(0);
		if (null == annc) {
			print(response, Msg.NOT_FIND);
			return;
		}
			
		annc.setDeleted(false);
		if (annCDAO.upd(annc) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.COLLECT)
	public void collect(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		AnnCollect annc = new AnnCollect();
		annc.setAc(Integer.parseInt(code));
		annc.setDeleted(false);
		annc.setUc(cu_code(request));
			
		if (annCDAO.add(annc) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}

	@Autowired
	public void setAnnCDAO(AnnCollectDAO annCDAO) {
		this.annCDAO = annCDAO;
	}
	@Autowired
	public void setAnnDAO(AnnouncementDAO annDAO) {
		this.annDAO = annDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
